package com.kedou.sdk.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.kedou.sdk.utils.ResUtils;

/**
 * Created by Administrator on 2017/6/6 0006.
 */

public class AlertDialog {

    final Builder builder;
    private final Dialog dialog;

    private AlertDialog(final Builder builder) {
        this.builder = builder;
        this.dialog = builder.dialog;
    }

    public void show() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public void setOnClickListener(Dialog.OnClickListener listener){
        builder.setOnClickListener(listener);
    }

    public void setCancelable(boolean flag){
        if(dialog != null){
            dialog.setCancelable(flag);
        }
    }

    public static class Builder implements View.OnClickListener {

        private Context context;
        private View contentView;
        private TextView message;
        private Button confirm;
        private Button cancel;
        private Dialog.OnClickListener listener;
        private Dialog dialog;

        public Builder(Context context) {
            this.context = context;
            final int layoutResId = ResUtils.getLayoutIdentifier(context, "wan_dialog_alert");
            final int msgResId = ResUtils.getViewIdentifier(context, "dialog_alert_messasge_tv");
            final int confirmResId = ResUtils.getViewIdentifier(context, "dialog_alert_confirm_btn");
            final int cancelResId = ResUtils.getViewIdentifier(context, "dialog_alert_cancel_btn");
            contentView = LayoutInflater.from(context).inflate(layoutResId, null);
            message = (TextView) contentView.findViewById(msgResId);
            confirm = (Button) contentView.findViewById(confirmResId);
            cancel = (Button) contentView.findViewById(cancelResId);
            confirm.setOnClickListener(this);
            cancel.setOnClickListener(this);
        }

        public Builder setMessage(String message) {
            if (TextUtils.isEmpty(message)) {
                this.message.setText("");
            } else {
                this.message.setText(message);
            }
            return this;
        }

        public Builder setConfirmText(String text) {
            if (!TextUtils.isEmpty(text)) {
                this.confirm.setText(text);
            }
            return this;
        }

        public Builder setCancelText(String text) {
            if (!TextUtils.isEmpty(text)) {
                this.cancel.setText(text);
            }
            return this;
        }

        public Builder setOnClickListener(Dialog.OnClickListener listener) {
            this.listener = listener;
            return this;
        }

        public AlertDialog create() {
            dialog = new Dialog(context, ResUtils.getStyleIdentifier(context, "hnssn_dialog"));
            dialog.setContentView(contentView);
            dialog.setCanceledOnTouchOutside(false);
            return new AlertDialog(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                if (v == confirm) {
                    listener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                } else if (v == cancel) {
                    listener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                } else {
                    listener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                }
            } else {
                dialog.dismiss();
            }
        }
    }
}
